import axios from 'axios';

// 创建带有基础URL的axios实例
const apiClient = axios.create({
  //baseURL: 'https://qilu.laoc.site/api/',
  baseURL: 'http://localhost:8080',
  responseType: 'blob' // 默认响应类型设为blob
});

export const downloadClient = async (
    url: string,
    params: Record<string, any>,
    filename: string,
    token?: string
): Promise<void> => {
  try {
    const response = await apiClient.get(url, { // 使用apiClient而不是axios
      params,
      headers: {
        Authorization: token || localStorage.getItem('token') || '',
      },
    });

    const blob = new Blob([response.data], {
      type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
    });

    const link = document.createElement('a');
    link.href = window.URL.createObjectURL(blob);
    link.download = filename;
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
  } catch (error) {
    console.error('下载 Excel 失败：', error);
    throw error;
  }
};